<?php
// +----------------------------------------------------------------------
// | ThinkPHP
// +----------------------------------------------------------------------
// | Copyright (c) 2007 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
// $Id$

// 配置模型
class StockModel extends CommonModel {

/* 	protected $_validate = array(
		array('name','require','参数名称必须'),
		array('name','checkName','参数已经定义',0,'callback'),
		);

	protected $_auto		=	array(
		array('create_time','time','function',self::MODEL_INSERT),
		);

	public function checkName() {
		$map['name']	 =	 $_POST['name'];
        if(!empty($_POST['id'])) {
			$map['id']	=	array('neq',$_POST['id']);
        }
		$result	=	$this->find(array('where'=>$map,'field'=>'id'));
        if($result) {
        	return false;
        }else{
			return true;
		}
	} */
	
	/**
	单个库存项目增加库存
	*/
	public function addstock($data=$_POST,$store=0){
		$vo=get_stock_by_goodsid($data['goodsid'],$store);
		$Stock=D('Stock');
		/**库存不存在*/
		if(!$vo['id']){
			//echo '库存项目不存在';
			/**
			插入新的库存项目
			*/
			$Stock->create($data);
			$vo['id']=$Stock->add();
		}
		/**
		修改库存
		*/
		$Stock->where("id=".$vo['id'])->setInc($vo['number']);
		return true;
	}
	
	/**
	根据goodsid 和store仓库编号获取库存信息
	基于 一个仓库只存在goods是唯一的
	*/
	public function get_stock_by_goodsid($goodsId,$store=0){
		$Stock=M('Stock');
		$map['goodsid']=$goodsId;
		$map['store']=$store;
		$vo=$Stock->where($map)->find();
		return $vo;
	}
	
	
	
}
?>